import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex);

/**
 * load modules
 */
const modulesContext = require.context("./modules", false, /\.js$/)
const modules = {}
modulesContext.keys().forEach(path => {
    const key = path.match(/\.\/(\w+)\.js$/)[1]
    modules[key] = modulesContext(path).default
})

export default new Vuex.Store({
    state: {
        // 认证身份
        identity: "user", // user, admin
        // 权限路由
        authRoutes: [],
        // 登录状态 -- 用于判断是否返回登陆页
        isLogin: false,
        // 会员信息
        userInfo: {},
    },

    mutations: {
        // 封装快捷设置全局store的方法
        SET_IDENTITY(state, data) {
            state.identity = data;
        },
        SET_AUTH_ROUTES(state, data) {
            state.authRoutes = data;
        },
        SET_LOGIN_STATUS(state, status) {
            state.isLogin = status;
        },
        SET_USER_INFO(state, data) {
            state.userInfo = data;
        }
    },
    actions: {},
    modules,
    plugins: [
        createPersistedState({
            storage: window.localStorage
        })
    ]
});
